﻿using System;
using System.Windows.Forms;
using PowerMonitor.Properties;

namespace PowerMonitor
{
    public partial class WaitingForm : Form
    {
        #region Properties
        private int _MaxWaitTime;
        private int _WaitTime;
        private bool _CancelEnable;
        private IAsyncResult _AsyncResult;
        private EventHandler<EventArgs> _Method;
        private bool _IsShown = true;
        private readonly int _EffectCount = 10;
        private readonly int _EffectTime = 500;
        /// <summary>
        /// 控制界面显示的特性
        /// </summary>
        private Timer _Timer;
        public string Message { get; private set; }
        public int TimeSpan { get; set; }
        public bool FormEffectEnable { get; set; }
        #endregion

        #region frmWaitingBox
      
        public WaitingForm(EventHandler<EventArgs> method)
        {
            int maxWaitTime = 120 * 1000;
            string waitMessage = Resources.txtPleaseWait;
            bool cancelEnable = true;
            bool timerVisable = true;
            Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable);
        }
        public WaitingForm(EventHandler<EventArgs> method, string waitMessage)
        {
            int maxWaitTime = 120 * 1000;
            bool cancelEnable = true;
            bool timerVisable = true;
            Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable);
        }
        public WaitingForm(EventHandler<EventArgs> method, bool timerVisable)
        {
            int maxWaitTime = 120 * 1000;
            string waitMessage = Resources.txtPleaseWait;
            bool cancelEnable = false;
            Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable);
        }

        public WaitingForm(EventHandler<EventArgs> method, bool cancelEnable, bool timerVisable)
        {
            int maxWaitTime = 120 * 1000;
            string waitMessage = Resources.txtPleaseWait;
            Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable);
        }
        public WaitingForm(EventHandler<EventArgs> method, string waitMessage, bool cancelEnable, bool timerVisable)
        {
            int maxWaitTime = 120 * 1000;
            //string waitMessage = "正在处理数据，请稍后...";
            Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable);
        }
        public WaitingForm(EventHandler<EventArgs> method, string waitMessage, bool cancelEnable, bool timerVisable, Int32 waitSecond)
        {
            int maxWaitTime;
            if (waitSecond <= 0)
            {
                maxWaitTime = 120 * 1000;
            }
            else
            {
                maxWaitTime = waitSecond * 1000;
            }
            //string waitMessage = "正在处理数据，请稍后...";
            Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable);
        }
        #endregion

        #region Initialize
        private void Initialize(EventHandler<EventArgs> method, int maxWaitTime, string waitMessage, bool cancelEnable, bool timerVisable)
        {
            InitializeComponent();
            //initialize form
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterParent;
            this.ShowInTaskbar = false;
            this.labMessage.Text = waitMessage;
            _Timer = new Timer();
            _Timer.Interval = _EffectTime / _EffectCount;
            _Timer.Tick += _Timer_Tick;
            this.Opacity = 0;
            FormEffectEnable = true;
            //para
            TimeSpan = 500;
            Message = string.Empty;
            _CancelEnable = cancelEnable;
            _MaxWaitTime = maxWaitTime;
            _WaitTime = 0;
            _Method = method;
            this.pictureBoxCancel.Visible = _CancelEnable;
            this.labTimer.Visible = timerVisable;
            this.timer1.Interval = TimeSpan;
            this.timer1.Start();
        }
        #endregion


        #region Events
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Message = Resources.txtWaitingFinish;
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                _WaitTime += TimeSpan;
                this.labTimer.Text = string.Format("{0}秒", _WaitTime / 1000);
                if (!this._AsyncResult.IsCompleted)
                {
                    if (_WaitTime > _MaxWaitTime)
                    {
                        Message = string.Format(Resources.txtWaitingTime, _MaxWaitTime / 1000);
                        this.Close();
                    }
                }
                else
                {
                    this.Message = string.Empty;
                    this.Close();
                }
            }
            catch
            {
            }
        }

        private void frmWaitingBox_Shown(object sender, EventArgs e)
        {
            try
            {
                _AsyncResult = _Method.BeginInvoke(null, null, null, null);
                //Effect
                if (FormEffectEnable)
                {
                    _Timer.Start();
                }
                else
                    this.Opacity = 1;
            }
            catch
            {
            }

        }
        private void frmWaitingBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (FormEffectEnable)
                {
                    if (this.Opacity >= 1)
                        e.Cancel = true;
                    _Timer.Start();
                }
            }
            catch
            {

            }

        }
        private void _Timer_Tick(object sender, EventArgs e)
        {
            try
            {
                if (_IsShown)
                {
                    if (this.Opacity >= 1)
                    {
                        _Timer.Stop();
                        _IsShown = false;
                    }
                    this.Opacity += 1.00 / _EffectCount;
                }
                else
                {
                    if (this.Opacity <= 0)
                    {
                        _Timer.Stop();
                        _IsShown = true;
                        this.Close();
                    }
                    this.Opacity -= 1.00 / _EffectCount;
                }
            }
            catch
            {

            }

        }
        #endregion
    }
}
